package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.Constants;
import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GoodsBean;
import com.example.administrator.familfinalcialmanagement.bean.StroeBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityTaskDescBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.GoodsAdapter;
import com.example.administrator.familfinalcialmanagement.ui.adapter.StoreAdapter;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/03/28 22:10
 */

public class TaskDescActivity extends BaseActivity{

    ActivityTaskDescBinding binding;
    String id,like,type,title;

    List<StroeBean.GoodsListBean> list = new ArrayList<>();
    List<StroeBean.GoodsListBean> listSecond = new ArrayList<>();
    StoreAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_task_desc);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();
        type = getIntent().getStringExtra( "module_type" );
        title = getIntent().getStringExtra( "module_title" );
        setTitleText(title);
        if ("id".equals( type )) {
            id = getIntent().getStringExtra( "module_id" );
            quertStroe(id,"");
        }else{
            like = getIntent().getStringExtra( "module_like" );
            quertStroe("",like);
        }

        adapter = new StoreAdapter(TaskDescActivity.this,list);
        binding.recycler.setLayoutManager(new LinearLayoutManager(TaskDescActivity.this));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener( new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent( TaskDescActivity.this,ToastActivity.class );
                intent.putExtra( "module_name",list.get(position).getName() );
                intent.putExtra( "module_factoryName",list.get(position).getType_name() );
                intent.putExtra( "module_img", Constants.BASE + list.get(position).getImg() );
                intent.putExtra( "module_count",list.get(position).getCount() + "");
                intent.putExtra( "module_id",list.get(position).getId() + "");
                intent.putExtra( "module_price",list.get( position ).getPrice() + "");
                intent.putExtra("module_bean",list.get(position));
                startActivity( intent );
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        } );

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

                default:
        }
    }


    public void quertStroe(String id,String like){
        ApiHTTP.getInstance().requestStore( "1", like, id, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                StroeBean bean = GsonUtil.GsonToBean( s,StroeBean.class );
                if (0 == bean.getCode()) {
                    listSecond = bean.getGoodsList();
                    list.clear();
                    list.addAll( listSecond );
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                ToastUtil.showToast( "联网失败" );
            }
        } );
    }

}
